package com.block.channel.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.channel.service.IChannelInfoService;
import com.block.channel.service.ICountInfoService;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.ProductConstant;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.model.dto.AppInfoDTO;
import com.block.common.model.dto.CommonReqDTO;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.IChannelCommonService;
import com.block.common.util.LocalDateUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelInfo;
import com.block.db.mapper.ChannelInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 渠道信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-08
 */
@Service
public class ChannelInfoServiceImpl extends ServiceImpl<ChannelInfoMapper, ChannelInfo> implements IChannelInfoService {

    @Autowired
    ICountInfoService countInfoService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IApplicationInfoService applicationInfoService;
    @Autowired
    IChannelCommonService channelCommonService;

    /**
     * 根据推广链接，返回产品的h5页面链接
     * @param ip
     * @return
     */
    @Override
    public String channelViewInfo(ChannelInfo channelInfo, String ip, HttpServletRequest request) {

        String channelCode = channelInfo.getChannelCode();

        // 获取落地页类型
        Long remainSecond = LocalDateUtil.getRemainSecondNumber();
        String redisKey = StrUtil.format(RedisKeyConstant.COUNT_UV_KEY, channelCode, ip);
        boolean result = stringRedisTemplate.opsForValue()
                .setIfAbsent(redisKey, System.currentTimeMillis()+"",remainSecond, TimeUnit.SECONDS);

        if (result){
            String random = UUID.fastUUID().toString(true);
            String userAgent = request.getHeader("User-Agent");
            // 3.统计渠道uv信息
            countInfoService.countUvDataInfo(channelCode, ip, userAgent, random);
        }

        String appType = "renxinhua";
        if (StrUtil.isNotEmpty(channelInfo.getAppType())){
            appType  = channelInfo.getAppType();
        }
        ApplicationInfo applicationInfo = applicationInfoService.getAppInfoByType(appType);

        // 判断返回链接的
        AppInfoDTO appInfoDTO = applicationInfoService.getDealAfterUrl(ip, applicationInfo);
        if (!appInfoDTO.isSelfFlag()){
            return appInfoDTO.getReqUrl();
        }

       String  hostUrl = appInfoDTO.getReqUrl() + "?ch="+ channelCode;

        return hostUrl;
    }

    /**
     * 根据渠道号获取，落地页的类型
     * @param commonReqDTO
     * @return
     */
    @Override
    public ResponseVo getPageTypeByCode(CommonReqDTO commonReqDTO) {

        ChannelInfo channelInfo = channelCommonService.getChannelInfoCache(commonReqDTO.getReqChannelCode());
        if (ObjectUtil.isEmpty(channelInfo)){
            channelInfo = this.getById(ProductConstant.DEFAULT_CHANNEL_CODE);
        }

        commonReqDTO.setReqChannelCode(channelInfo.getChannelCode());
        commonReqDTO.setReqChannelName(channelInfo.getChannelName());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageType", channelInfo.getPageType());
        jsonObject.put("smsVerifyFlag", channelInfo.getSmsVerifyFlag());
        jsonObject.put("chargeType", channelInfo.getChargeType());

        HashMap<String, Object> zhimaSourceMap = new HashMap<>();
//        zhimaSourceMap.put(BaseTypeInfoEnum.ZhimaScore.zm001.getValue(), BaseTypeInfoEnum.ZhimaScore.zm001.getName());
        zhimaSourceMap.put(BaseTypeInfoEnum.ZhimaScore.zm002.getValue(), BaseTypeInfoEnum.ZhimaScore.zm002.getName());
        zhimaSourceMap.put(BaseTypeInfoEnum.ZhimaScore.zm003.getValue(), BaseTypeInfoEnum.ZhimaScore.zm003.getName());
        zhimaSourceMap.put(BaseTypeInfoEnum.ZhimaScore.zm004.getValue(), BaseTypeInfoEnum.ZhimaScore.zm004.getName());
        zhimaSourceMap.put(BaseTypeInfoEnum.ZhimaScore.zm005.getValue(), BaseTypeInfoEnum.ZhimaScore.zm005.getName());
        jsonObject.put("zhimaSourceMap", zhimaSourceMap);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }

}
